import 'dart:convert';

import 'package:card_swiper/card_swiper.dart';
import 'package:crypto/crypto.dart';
import 'package:distribution_mall_flutter/constant/cf_constant.dart';
import 'package:distribution_mall_flutter/generated/l10n.dart';
import 'package:distribution_mall_flutter/http/modules/user.dart';
import 'package:distribution_mall_flutter/provider/user/login.dart';
import 'package:distribution_mall_flutter/utils/common_widget.dart';
import 'package:distribution_mall_flutter/widgets/agreement.dart';
import 'package:distribution_mall_flutter/widgets/icon_button.dart';
import 'package:distribution_mall_flutter/widgets/third_party_login.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:getwidget/getwidget.dart';

class LoginSwiper extends ConsumerWidget {
  LoginSwiper({super.key});

  // final loginController = Get.find<LoginController>();
  final List<Widget> loginType = [
    Tab(
      height: 30,
      child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: EdgeConstant.edge15),
          child: Text(
            S.current.user_login_mobileNumLogin,
          )),
    ),
    Tab(
        height: 30,
        child: Padding(
            padding:
                const EdgeInsets.symmetric(horizontal: EdgeConstant.edge15),
            child: Text(
              S.current.user_login_accLogin,
            ))),
  ];

  Widget inputRender(
      {required BuildContext context,
      Widget? prefixIcon,
      String hintText = '',
      Widget? suffixIcon,
      bool obscureText = false,
      EdgeInsets contentPadding = const EdgeInsets.fromLTRB(12, 24, 12, 16),
      TextInputType keyboardType = TextInputType.phone}) {
    return SizedBox(
      width: MediaQuery.of(context).size.width - 19 - 19,
      height: 46,
      child: TextField(
        inputFormatters: [LengthLimitingTextInputFormatter(12)],
        textAlign: TextAlign.left,
        textAlignVertical: TextAlignVertical.center,
        obscureText: obscureText,
        decoration: InputDecoration(
          hintText: hintText,
          prefixIcon: prefixIcon,
          suffixIcon: suffixIcon,
          contentPadding: contentPadding,
          enabledBorder: const OutlineInputBorder(
              borderSide: BorderSide(color: ColorConstant.grey233, width: 0.9),
              borderRadius:
                  BorderRadius.all(Radius.circular(RadiusConstant.edge10))),
          errorBorder: const OutlineInputBorder(
              borderSide:
                  BorderSide(color: Color.fromRGBO(255, 0, 0, 1), width: 0.9),
              borderRadius:
                  BorderRadius.all(Radius.circular(RadiusConstant.edge10))),
          focusedBorder: const OutlineInputBorder(
              borderSide: BorderSide(color: Colors.black, width: 0.9),
              borderRadius:
                  BorderRadius.all(Radius.circular(RadiusConstant.edge10))),
          hintStyle: const TextStyle(
              color: ColorConstant.grey153,
              textBaseline: TextBaseline.alphabetic, // 设置文本基线为基准线
              height: 1),
        ),
        keyboardType: keyboardType,
        style: const TextStyle(
          fontSize: FontConstant.f16, // 设置字体大小为20
        ),
        onChanged: (str) {},
      ),
    );
  }

  Widget mobileRender({required BuildContext context}) {
    return Column(
      children: [
        inputRender(
          context: context,
          prefixIcon: const SizedBox(
            width: 40,
            child: Align(
              alignment: Alignment.center,
              child: Text(
                '+63',
                textAlign: TextAlign.center,
              ),
            ),
          ),
          hintText: S.current.user_login_enterMobile,
        ),
        const SizedBox(
          height: 10,
        ),
        inputRender(
            context: context,
            hintText: S.of(context).user_login_pleaseVerif,
            contentPadding: const EdgeInsets.fromLTRB(10, 24, 12, 16),
            keyboardType: TextInputType.number,
            suffixIcon: SizedBox(
              width: 109,
              child: Row(
                children: [
                  const VerticalDivider(
                    color: ColorConstant.grey233,
                    width: 1,
                    indent: 12,
                    endIndent: 12,
                  ),
                  const SizedBox(
                    width: 9,
                  ),
                  CommonBtnWidget.textBtn(
                      onPressed: () {},
                      text: S.of(context).user_login_sendSMS,
                      textStyle: const TextStyle(
                          color: ColorConstant.primaryColor,
                          fontSize: FontConstant.f14))
                ],
              ),
            ))
      ],
    );
  }

  Widget accountRender({required BuildContext context}) {
    return Column(
      children: [
        inputRender(
          context: context,
          hintText: '',
          keyboardType: TextInputType.name,
        ),
        const SizedBox(
          height: 10,
        ),
        inputRender(
            context: context,
            keyboardType: TextInputType.visiblePassword,
            hintText: 'user.login.password',
            // obscureText: loginController.state.isEye ? true : false,
            contentPadding: const EdgeInsets.fromLTRB(10, 24, 12, 16),
            suffixIcon: EPIconButton(
              onPressed: () {
                // loginController.changeEye();
              },
              // icon: Icon(false //loginController.state.isEye
              //     ? Icons.visibility_off_rounded
              //     : Icons.visibility_rounded),
            ))
      ],
    );
  }

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final modal = ref.watch(loginProvider);

    return Expanded(
      child: Column(
        children: [
          Container(
            alignment: Alignment.center,
            margin: const EdgeInsets.only(
                left: EdgeConstant.edge10,
                right: EdgeConstant.edge10,
                bottom: EdgeConstant.edge30),
            padding: const EdgeInsets.symmetric(vertical: EdgeConstant.edge3),
            decoration: const BoxDecoration(
                color: ColorConstant.grey233,
                borderRadius:
                    BorderRadius.all(Radius.circular(RadiusConstant.edge10))),
            child: DefaultTabController(
              length: loginType.length,
              child: TabBar(
                onTap: (int index) {
                  ref.read(loginProvider.notifier).changeTabIndex(index);
                },
                labelPadding: const EdgeInsets.all(0),
                labelStyle: const TextStyle(
                    fontSize: FontConstant.f14,
                    fontWeight: FontWeight.bold,
                    color: Colors.black),
                unselectedLabelStyle: const TextStyle(
                    fontSize: FontConstant.f14,
                    fontWeight: FontWeight.normal,
                    color: Colors.black),
                indicatorColor: Colors.white,
                indicator: const BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(10))),
                tabs: loginType,
              ),
            ),
          ),
          Expanded(
              child: Column(
            children: [
              SizedBox(
                height: 130,
                child: PageView(
                  controller: modal.controller,
                  physics: const NeverScrollableScrollPhysics(),
                  children: <Widget>[
                    mobileRender(context: context),
                    accountRender(context: context)
                  ],
                ),
              ),
              const Padding(
                padding: EdgeInsets.only(left: 19),
                child: EPAgreement(),
              ),
              const SizedBox(
                height: 20.5,
              ),
              GFButton(
                onPressed: () {
                  List<int> inputBytes = utf8.encode('Qw123123');
                  String md5Digest = md5.convert(inputBytes).toString();
                  UserApi.loginAccount(
                      {'account': 'lude1', 'password': md5Digest});
                },
                text: 'loginIn',
                shape: GFButtonShape.pills,
                size: GFSize.LARGE,
                blockButton: true,
                textStyle: const TextStyle(
                    fontSize: FontConstant.f14,
                    fontWeight: FontWeight.bold,
                    color: Colors.white),
                color: ColorConstant.primaryColor,
              ),
              CommonBtnWidget.textBtn(
                  onPressed: () {},
                  highlightElevation: 0,
                  hideHightLight: true,
                  text: S.current.user_forgot,
                  textStyle: const TextStyle(
                      fontSize: FontConstant.f12, color: ColorConstant.grey153))
            ],
          )),
          const ThirdPartyLogin()
        ],
      ),
    );
  }
}
